package assess

import (
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/assess"
	assessReq "gitcode.com/G-YT/ANN/server/model/assess/request"
	"gitcode.com/G-YT/ANN/server/model/common/request"
	"gitcode.com/G-YT/ANN/server/model/common/response"
	"gitcode.com/G-YT/ANN/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

type CustomerApi struct{}

// CreateCustomer 创建客户档案
// @Tags Customer
// @Summary 创建客户档案
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body assessReq.CreateCustomerRequest true "客户档案信息"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /assess/customer/createCustomer [post]
func (customerApi *CustomerApi) CreateCustomer(c *gin.Context) {
	var req assessReq.CreateCustomerRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 验证必填字段
	if req.Phone == "" {
		response.FailWithMessage("手机号不能为空", c)
		return
	}
	if req.Name == "" {
		response.FailWithMessage("姓名不能为空", c)
		return
	}
	if len([]rune(req.Name)) > 4 {
		response.FailWithMessage("姓名不能超过4个字", c)
		return
	}
	if req.Cm <= 0 {
		response.FailWithMessage("身高必须大于0", c)
		return
	}
	if req.Kg <= 0 {
		response.FailWithMessage("体重必须大于0", c)
		return
	}
	if req.WaistLine <= 0 {
		response.FailWithMessage("腰围必须大于0", c)
		return
	}
	if req.HipLine <= 0 {
		response.FailWithMessage("臀围必须大于0", c)
		return
	}

	// 获取健管师ID
	HmID := utils.GetHmID(c)

	// 转换为数据模型
	customer := assess.Customer{
		HmID:      HmID,
		Phone:     req.Phone,
		Name:      req.Name,
		Sex:       req.Sex,
		Birthday:  req.Birthday,
		Cm:        req.Cm,
		Kg:        req.Kg,
		Waistline: req.WaistLine,
		Hipline:   req.HipLine,
	}

	// 调用服务层创建客户档案
	if err = CustomerService.CreateCustomer(customer); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("创建成功", c)
}

// GetCustomerList 分页获取客户档案列表
// @Tags Customer
// @Summary 分页获取客户档案列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body assessReq.CustomerSearch true "查询参数"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /assess/customer/getCustomerList [post]
func (customerApi *CustomerApi) GetCustomerList(c *gin.Context) {
	var pageInfo assessReq.CustomerSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 验证分页参数
	if err := utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 获取健管师ID
	HmID := utils.GetHmID(c)
	pageInfo.HmID = HmID

	// 调用服务层获取客户档案列表
	list, total, err := CustomerService.GetCustomerList(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取客户(档案)列表失败!", zap.Error(err))
		response.FailWithMessage("获取失败: "+err.Error(), c)
		return
	}

	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetCustomerById 根据ID获取客户档案
// @Tags Customer
// @Summary 根据ID获取客户档案
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id path int true "客户档案ID"
// @Success 200 {object} response.Response{data=assess.Customer,msg=string} "获取成功"
// @Router /assess/customer/getCustomer [get]
func (customerApi *CustomerApi) GetCustomerById(c *gin.Context) {
	idStr := c.Query("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.FailWithMessage("ID格式错误", c)
		return
	}

	// 调用服务层获取客户档案
	customer, err := CustomerService.GetCustomerById(uint(id))
	if err != nil {
		global.GVA_LOG.Error("获取客户档案失败!", zap.Error(err))
		response.FailWithMessage("获取失败: "+err.Error(), c)
		return
	}

	response.OkWithDetailed(customer, "获取成功", c)
}

// UpdateCustomer 更新客户档案
// @Tags Customer
// @Summary 更新客户档案
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body assess.Customer true "客户档案信息"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /assess/customer/updateCustomer [put]
func (customerApi *CustomerApi) UpdateCustomer(c *gin.Context) {
	var customer assess.Customer
	err := c.ShouldBindJSON(&customer)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 验证必填字段
	if customer.Phone == "" {
		response.FailWithMessage("手机号不能为空", c)
		return
	}
	if customer.Name == "" {
		response.FailWithMessage("姓名不能为空", c)
		return
	}
	if len([]rune(customer.Name)) > 4 {
		response.FailWithMessage("姓名不能超过4个字符", c)
		return
	}
	if customer.Cm <= 0 {
		response.FailWithMessage("身高必须大于0", c)
		return
	}
	if customer.Kg <= 0 {
		response.FailWithMessage("体重必须大于0", c)
		return
	}
	if customer.Waistline <= 0 {
		response.FailWithMessage("腰围必须大于0", c)
		return
	}
	if customer.Hipline <= 0 {
		response.FailWithMessage("臀围必须大于0", c)
		return
	}
	hmID := utils.GetHmID(c)
	customer.HmID = hmID

	// 调用服务层更新客户档案
	if err := CustomerService.UpdateCustomer(customer); err != nil {
		global.GVA_LOG.Error("更新客户档案失败!", zap.Error(err))
		response.FailWithMessage("更新失败: "+err.Error(), c)
		return
	}

	response.OkWithMessage("更新成功", c)
}

// DeleteCustomer 删除客户档案
// @Tags Customer
// @Summary 删除客户档案
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id path int true "客户档案ID"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /assess/customer/deleteCustomer/{id} [delete]
func (customerApi *CustomerApi) DeleteCustomer(c *gin.Context) {
	idStr := c.Query("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.FailWithMessage("ID格式错误", c)
		return
	}

	// 调用服务层删除客户档案
	if err := CustomerService.DeleteCustomer(uint(id)); err != nil {
		global.GVA_LOG.Error("删除客户档案失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}

	response.OkWithMessage("删除成功", c)
}

// DeleteCustomerByIds 批量删除客户档案
// @Tags Customer
// @Summary 批量删除客户档案
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "客户档案ID列表"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /assess/customer/deleteCustomerByIds [delete]
func (customerApi *CustomerApi) DeleteCustomerByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 调用服务层批量删除客户档案
	if err := CustomerService.DeleteCustomerByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除客户档案失败!", zap.Error(err))
		response.FailWithMessage("删除失败: "+err.Error(), c)
		return
	}

	response.OkWithMessage("删除成功", c)
}
